package cn.tedu._03weibo_test.controller;

import cn.tedu._03weibo_test.base.response.JsonResult;
import cn.tedu._03weibo_test.base.response.StatusCode;
import cn.tedu._03weibo_test.base.utils.CacheUtil;
import cn.tedu._03weibo_test.mapper.WeiboMapper;
import cn.tedu._03weibo_test.pojo.entity.Weibo;
import cn.tedu._03weibo_test.pojo.vo.UserVO;
import cn.tedu._03weibo_test.pojo.vo.WeiboVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "2.微博功能模块")
@RequestMapping("/v1/weibo/")
@RestController
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @Autowired
    private CacheUtil cacheUtil;
    @GetMapping("insert")
    @ApiOperation("发布微博")
    @ApiOperationSupport(order = 10)
    public JsonResult insertWeibo(String content, @ApiIgnore HttpSession session){
        log.debug("开始发布微博：");
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Weibo weibo = new Weibo();
        weibo.setContent(content); // 设置 content
        weibo.setUserId(userVO.getId()); // 设置 user_id
        weibo.setCreateTime(new Date()); // 设置 create_time
        weiboMapper.insertWeibo(content, weibo.getCreateTime(), weibo.getUserId());
        return JsonResult.ok();
    }
    @GetMapping("selectIndex")
    @ApiOperation("首页列表")
    @ApiOperationSupport(order = 20)
    public JsonResult listId(){
        log.debug("首页列表展示：");
        Serializable weiboIndex = cacheUtil.get("weibo_index");
        if (weiboIndex != null){
            log.debug("首页数据来自redis数据库。。。");
            return JsonResult.ok(weiboIndex);
        }
        log.debug("首页数据来自MySQL数据库。。。");
        List<WeiboVO> weiboVOS = weiboMapper.selectWeiboos();
        if (weiboVOS == null){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        cacheUtil.setExpire("weibo_index",(Serializable) weiboVOS,30);
        return JsonResult.ok(weiboVOS);
    }
    @GetMapping("selectById")
    @ApiOperation("微博详情页")
    @ApiOperationSupport(order = 30)
    public JsonResult weiboSelectById(Long id,@ApiIgnore HttpSession session){
        log.debug("微博详情页");
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Weibo weibo = weiboMapper.selectById(id);
        if (weibo == null){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        return JsonResult.ok(weibo);
    }
}
